using ApiLib.RCL.Components.AutoTable.Attributes;
using ApiLib.RCL.Components.AutoTable.Models;
using ApiLib.RCL.Utils;
using BootstrapBlazor.Components;

namespace ApiLib.RCL.Components.AutoTable.Utils;

/// <summary>
/// 列工具类
/// </summary>
public static class ColumnUtil
{

    /// <summary>
    /// 获取时间格式化
    /// </summary>
    /// <param name="column"></param>
    /// <returns></returns>
    public static string GetDateFormat(this ColumnAttribute column)
    {
        return column.ShowType switch
        {
            "year" => "yyyy",
            "month" => "yyyy-MM",
            "date" => "yyyy-MM-dd",
            _ => "yyyy-MM-dd HH:mm:ss"
        };
    }
    /// <summary>
    /// 获取时间模式
    /// </summary>
    /// <param name="column"></param>
    /// <returns></returns>
    public static DatePickerViewMode GetDateMode(this ColumnAttribute column)
    {
        return column.ShowType switch
        {
            "year" => DatePickerViewMode.Year,
            "month" => DatePickerViewMode.Month,
            "date" => DatePickerViewMode.Date,
            _ => DatePickerViewMode.DateTime
        };
    }
    /// <summary>
    /// 根据属性名获取值
    /// </summary>
    /// <param name="column"></param>
    /// <param name="obj">对象</param>
    /// <param name="fieldName">属性名</param>
    /// <returns></returns>
    public static object? GetFieldValue(this ColumnAttribute column,object obj, string fieldName)
    {
        var value = obj.GetFieldValue(fieldName);
        if (column.FieldType is TableFieldType.Date && value is DateTime date)
        {
            return date.ToString(column.GetDateFormat());
        }
        return value;
    }
}